#*****************************************************************************************************
#* FILE: arithmetic-expressions.asm
#*
#* DECRIPTION
#* Write a subroutine that takes three arguments and computes some functions
#*
#* AUTHORS
#* Joe Boeding (Joe.Boeding@asu.edu) [JB]
#* Taylor Wood (Taylor.D.Wood@asu.edu) [TW]
#*
#* Computer Science & Engineering
#* CSE420 Computer Architecture I, Spring 2014
#*
#* MODIFICATION HISTORY
#* --------------------------------------------------------------------------------------------------
#****************************************************************************************************

# Create strings for user prompts
.data	
	askU:		.asciiz		"Please enter a value for U: "
	askV:		.asciiz		"Please enter a value for V: "
	strResult:	.asciiz		"The result of 3u^2-9uv+7v^2 = : "

# Main program
.text
	
# Step 1 - Ask for U and V values

	la $a0, askU		# promt to ask for u value
	li $v0, 4		# command to print string
	syscall			# execute system call
	
	li $v0, 5		# command to read int
	syscall			# execute system call
	move $s0, $v0		# store u in save register 0
	
	la $a0, askV		# prompt to ask for v value
	li $v0, 4		# command to print string
	syscall			# execute system call
	
	li $v0, 5		# command to read int
	syscall			# execute system call
	move $s1, $v0		# store v in save register 1
	
# Step 2 - Perform calculation 3u^2
	li $a0, 3		# load 3 in argument 1
	move $a1, $s0		# load u in argument 2
	move $a2, $s0		# load u in argument 3
	jal Multiply		# perform multiplication
	move $s2, $v0		# store result in save register 2
	
	# perform calculation 9uv
	li $a0, 9		# load 9 in argument 1
	move $a1, $s0		# load u in argument 2
	move $a2, $s1		# load v in argument 3
	jal Multiply		# perform multiplication
	move $s3, $v0		# store result in save register 3

	# perform calculation 7v^2
	li $a0, 7		# load 7 in argument 1
	move $a1, $s1		# load v in argument 2
	move $a2, $s1		# load v in argument 3
	jal Multiply		# perform multiplicataion
	move $s4, $v0		# store result in save register 4
	
	# perform calculation $s2 - $s3 + $s4
	sub $s5, $s2, $s3  	# substract $s3 from $s2 and store in save register 5
	add $s5, $s5, $s4	# add previous result with $s4 and store in save register 5
	
	# print result string
	la $a0, strResult	# promt to indicate result
	li $v0, 4		# command to print string
	syscall			# execute system call

	move $a0, $s5		# load result in the argument register
	li $v0, 1		# load print syscall command
	syscall			# print an integer

	li $v0, 10		# command to end the program
	syscall			# execute the system call

	
#* --------------------------------------------------------------------------------------------------
#* FUNCTION: Multiply(int $a0, int $a1, int $a2)
#*
#* DESCRIPTION
#* 	$a0 - (int) A
#*	$a1 - (int) X
#*	$a2 - (int) Y
#* 	$v0 - (int) result
#* --------------------------------------------------------------------------------------------------
Multiply:
		
	# multiply A and X
	mult $a0, $a1		# multiply arguments 0 and 1
	mflo $v0		# only take integer part
	
	# multiply AX and Y
	mult $v0, $a2		# multiply previous result and argument 2
	mflo $v0		# save value in return result

	jr $ra			# return to caller
